package com.bobo.web.provider.util.commutils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class EMail {
    private static final Logger logger = LoggerFactory.getLogger(EMail.class.getSimpleName());
//
//    private static ExecutorService executorService = Executors.newFixedThreadPool(20);
//    private static Session session;
//
//    static Properties properties = System.getProperties();
//
//    static {
//
//        properties.setProperty("mail.transport.protocol", "smtp");
//        properties.setProperty("mail.smtp.auth", "true");
//        properties.setProperty("mail.smtp.host", CltWebConstant.EMail.HOST);
//        properties.setProperty("mail.smtp.port", CltWebConstant.EMail.PORT);
//
//        session = Session.getInstance(properties, new Authenticator() {
//            protected PasswordAuthentication getPasswordAuthentication() {
//                return new PasswordAuthentication(CltWebConstant.EMail.USERNAME, CltWebConstant.EMail.PASSWORD);
//            }
//        });
//
//
//    }
//
//
//    // 向员工发送重置密码邮件
//    public static void sendResetStaffPasswordEmail(String email, String password) {
//        AbstractTemplates template = new EmailTemplates();
//        Map<String, Object> paramsMap = new HashMap<String, Object>();
//        paramsMap.put("password", password);
//        String subject = template.render(EmailTemplates.ResetStaffPasswordEmailTemplate.SUBJECT, paramsMap);
//        String content = template.render(EmailTemplates.ResetStaffPasswordEmailTemplate.CONTENT, paramsMap);
//
//        send(CltWebConstant.EMail.FROM, email, subject, content);
//    }
//
//    private static void send(final String from, final String to, final String subject, final String content) {
//        send(from, to, null/*no cc*/, subject, content);
//    }
//
//    private static void send(final String from, final String to, final String cc, final String subject, final String content) {
//        if (CltWebConstant.ENABLE_EMAIL) {
//            checkSubjectIsNull(subject);
//            checkContentIsNull(content);
////            if (!NetworkUtils.checkMacAddressIsValidated()) {
////                logger.error("try to send email from: " + NetworkUtils.getMacAddress() + ", but dennied");
////                return;
////            }
//            final String contentAddMacInfo = writeMacAddressToEmailContent(content);
//            logger.info("before send email task submit, from {} to {} cc {} msg:{}",from,to,cc,contentAddMacInfo);
//            FutureTask<Boolean> futureTask = new FutureTask<Boolean>(new Callable<Boolean>() {
//                @Override
//                public Boolean call() throws Exception {
//                    logger.info("send email start, from {} to {} cc {} msg:{}",from,to,cc,contentAddMacInfo);
//                    int count = 0;
//                    while (count < 10) {
//                        logger.info("try sending email round{}",count);
//                        try {
//                            MimeMessage message = new MimeMessage(session);
//                            message.setFrom(new InternetAddress(from));
//                            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
//                            if (cc != null) {
//                                message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
//                            }
//                            message.setSubject(MimeUtility.encodeText(subject, "UTF-8", "B"));
//                            message.setContent(contentAddMacInfo, "text/html;charset=utf-8");
//                            Transport.send(message);
//                            logger.info("sending email end at round{}",count);
//                            break;
//                        } catch (Exception e) {
//                            count++;
//                            logger.error("Error when sending email from {} to {} with exception {}", from, to, e);
//                            Thread.sleep(3000);
//                        }
//                    }
//                    if (count == 10) {
//                        logger.error("Error when sending email from {} to {} with error", from, to);
//                    }else {
//                        logger.info("sending email from {} to {} with success", from, to);
//                    }
//                    logger.info("send email end");
//                    return true;
//                }
//            });
//            executorService.submit(futureTask);
//            logger.info("send email task submited");
//        } else {
//            logger.info("Debug mode, skip EMAIL Sending");
//            logger.info("Debug mode, EMAIL: {}; Subject: {}; Content: {}", to, subject, content);
//        }
//    }
//
//    private static void checkSubjectIsNull(String subject) {
//        if (subject == null) {
//            throw new IllegalStateException("email subject is null.");
//        }
//    }
//
//    private static void checkContentIsNull(String content) {
//        if (content == null) {
//            throw new IllegalStateException("email content is null.");
//        }
//    }
//
//    // 记录发送邮件的mac地址在邮件中(隐藏显示) creator:wangzhiquan 2015-12-3
//    private static String writeMacAddressToEmailContent(String content) {
//        String currentMacAddress = NetworkUtils.getMacAddress();
//        StringBuilder macAddressInfo = new StringBuilder();
//        macAddressInfo.append("<p style='display: none;'>")
//                .append("Mac address : ")
//                .append(currentMacAddress == null ? "" : currentMacAddress)
//                .append("</p>");
//        String contentAddMacInfo = content + macAddressInfo.toString();
//        if (ArrayUtils.contains(CltWebConstant.DefaultMacAddress.PRODUCE, currentMacAddress)) {
//            // 默认mac地址中发送邮件
//            logger.info("send mail form server:" + currentMacAddress == null ? "" : currentMacAddress);
//        } else {
//            // 不在默认mac地址中发送邮件
//            logger.error("send mail form server:" + currentMacAddress == null ? "" : currentMacAddress);
//        }
//        return contentAddMacInfo;
//    }

}
